package com.foxzzz.back.tracking;

import java.util.ArrayList;
import java.util.List;

public class Solution93 {
    public List<String> restoreIpAddresses(String s) {
        int i=s.length();
        List<String> result=new ArrayList<>();
        ip(result,"",s,0);
        return result;
    }

    public void ip(List<String> result,String ip,String s,int level){
        if(ip.length()==s.length()+level&&level==4){
            result.add(new String(ip.substring(0,ip.length()-1)));
            return;
        }
        //裁剪 level 1.2.3.4
        if(s.length()+level-ip.length()>(4-level)*3)
            return;
        for (int i=1;i<=3;i++){
           int start=ip.length()-level;
           int end=start+i;
           if(start+i>s.length())
               continue;
           String ipsub=s.substring(start,end);
           int num=Integer.parseInt(ipsub);
           //去除前面是0的情况
           if(num<=255&&(String.valueOf(num).length()==ipsub.length())){
               ip=ip+ipsub+".";
               ip(result,ip,s,level+1);
               ip=ip.substring(0,ip.length()-String.valueOf(num).length()-1);
           }
        }


    }

    public static void main(String[] args) {
        System.out.println(new Solution93().restoreIpAddresses("010010"));
    }
}
